package cn.lingyangwl.agile.food.core.controller.admin;

import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import cn.lingyangwl.agile.food.core.service.GoodsService;
import cn.lingyangwl.agile.food.model.rqrs.goods.GoodsPageReq;
import cn.lingyangwl.agile.food.model.rqrs.goods.GoodsResp;
import cn.lingyangwl.agile.food.model.rqrs.goods.GoodsSaveReq;
import cn.lingyangwl.agile.food.model.rqrs.goods.GoodsUpdateReq;
import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.framework.security.xss.core.XssCleanIgnore;
import cn.lingyangwl.framework.validation.group.UpdateGroup;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * 商品
 * 
 * @author shenguangyang
 */
@Api(tags = "商品")
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    @ApiOperation(value = "分页总店商品")
    @GetMapping("/page")
    @RamResource(action = "goods:info:query", name = "查询商品", type = ActionType.READ)
    public Resp<IPage<GoodsResp>> pageGoods(@Validated GoodsPageReq req) {
        return Resp.ok(goodsService.pageGoods(req));
    }

    @ApiOperation(value = "分页门店商品")
    @GetMapping("/pageStoreGoods")
    @RamResource(action = "goods:info:query", name = "查询商品", type = ActionType.READ)
    public Resp<IPage<GoodsResp>> pageStoreGoods(@Validated GoodsPageReq req) {
        return Resp.ok(goodsService.pageCurrentStoreGoods(req));
    }

    @ApiOperation(value = "通过id查找商品")
    @GetMapping("/get")
    @RamResource(action = "goods:info:query", name = "查询商品", type = ActionType.READ)
    public Resp<GoodsResp> getGoods(@RequestParam(value = "id") Long id) {
        return Resp.ok(goodsService.getGoods(id));
    }
  
    @ApiOperation(value = "保存商品")
    @PostMapping("/save")
    @RamResource(action = "goods:info:save", name = "保存商品", type = ActionType.WRITE)
    public Resp<?> saveGoods(@RequestBody GoodsSaveReq req) {
        return Resp.ok(goodsService.saveGoods(req));
    }
  
    @ApiOperation(value = "更新商品")
    @PostMapping("/update")
    @XssCleanIgnore({"detailContent"})
    @RamResource(action = "goods:info:update", name = "更新商品", type = ActionType.WRITE)
    public Resp<?> updateGoods(@Validated(UpdateGroup.class) @RequestBody GoodsUpdateReq req) {
        return Resp.ok(goodsService.updateGoods(req));
    }
  
    @ApiOperation(value = "批量删除商品(总和子店公用)")
    @DeleteMapping("/batchDelete")
    @RamResource(action = "goods:info:delete", name = "删除商品", type = ActionType.WRITE)
    public Resp<?> batchDeleteGoods(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(goodsService.batchDeleteGoods(ids));
    }
  
    @ApiOperation(value = "删除商品(总和子店公用)")
    @DeleteMapping("/delete")
    @RamResource(action = "goods:info:delete", name = "删除商品", type = ActionType.WRITE)
    public Resp<?> deleteGoods(@RequestParam(value = "id") Long id) {
        return Resp.ok(goodsService.deleteGoods(id));
    }

    @ApiOperation(value = "更新商品上线门店(总店)")
    @PostMapping("/updatePublishStatus")
    @RamResource(action = "goods:info:updatePublishStatus", name = "更新商品上线门店(总店)", type = ActionType.WRITE)
    public Resp<?> updatePublishStatus(@RequestParam(value = "id") Long id,
                                      @RequestParam(value = "tenantIds") Set<String> tenantIds) {
        goodsService.updatePublishStatus(id, tenantIds);
        return Resp.ok();
    }

    @ApiOperation(value = "更新商品上线门店(子店)")
    @PostMapping("/updateStoreGoodsPublishStatus")
    @RamResource(action = "goods:info:updateStoreGoodsPublishStatus", name = "更新商品上线门店(子店)", type = ActionType.WRITE)
    public Resp<?> updateStoreGoodsPublishStatus(@RequestParam(value = "id") Long id,
                                                 @RequestParam(value = "isPublish") Boolean isPublish) {
        goodsService.updateStoreGoodsPublishStatus(Collections.singletonList(id), isPublish);
        return Resp.ok();
    }

    @ApiOperation(value = "批量添加商品到当前门店")
    @PostMapping("/batchAddIntoCurrentStore")
    @RamResource(action = "goods:info:batchAddIntoCurrentStore", name = "批量添加商品到当前门店", type = ActionType.WRITE)
    public Resp<?> batchAddIntoCurrentStore(@RequestParam("goodsIds") Set<Long> goodsIds) {
        goodsService.batchAddIntoCurrentStore(goodsIds);
        return Resp.ok();
    }

    @ApiOperation(value = "批量删除商品到当前门店")
    @DeleteMapping("/batchDeleteFromCurrentStore")
    @RamResource(action = "goods:info:batchDeleteFromCurrentStore", name = "从当前门店批量删除商品", type = ActionType.WRITE)
    public Resp<?> batchDeleteFromCurrentStore(@RequestParam("goodsIds") List<Long> goodsIds) {
        goodsService.batchDeleteFromCurrentStore(goodsIds);
        return Resp.ok();
    }
}
